package com.miao.controller;

import com.miao.dto.UserDto;
import com.miao.entity.UserEntity;
import com.miao.service.UserService;
import com.miao.util.JSONUtils;
import com.miao.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@Tag(name = "用户服务")
@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("page")
    @Operation(summary = "用户列表")
    @Parameters({
            @Parameter(name = "pageSize", description = "分页限制", required = true),
            @Parameter(name = "currentPage", description = "页码", required = true)
    })
    public Result<Object> page(@RequestParam(required = false) Map<String, Object> params) {
        return userService.Page(params);
    }

    @DeleteMapping("{id}")
    @Parameter(name = "id",description = "被删除用户id",required = true)
    @Operation(summary = "删除用户")
    public Result<Object> deleteUserById(@PathVariable("id") Long id){
        userService.removeById(id);
        return new Result<>();
    }

    @Operation(summary = "新增用户")
    @PostMapping("save")
    public Result<Object> save(@RequestBody UserDto dto){
        return userService.save(dto);
    }

}
